rust-guard: eliminate hot-path allocations in file secrecy and baseline scope inference#5679
Merged
Merged
Conversation
Copilot
AI
changed the title
[WIP] Fix avoidable heap allocations in check_file_secrecy
rust-guard: eliminate hot-path allocations in file secrecy and baseline scope inference
May 14, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces allocations in Rust Guard labeling hot paths by using borrowed scope strings where possible and avoiding segment collection during file secrecy checks.
Changes:
infer_scope_for_baselinenow returnsCow<str>and borrows common baseline scopes.- File secrecy path checks avoid collecting split segments into a
Vec. - Tests add assertions for borrowed vs owned baseline inference behavior.
Show a summary per file
| File | Description |
|---|---|
guards/github-guard/rust-guard/src/lib.rs |
Updates baseline scope inference to use Cow and adds branch-level tests. |
guards/github-guard/rust-guard/src/labels/tool_rules.rs |
Replaces eager path segment collection with lazy path splitting. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 2
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This was referenced May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✨ Enhancement
This change removes two avoidable heap allocations in Rust Guard hot paths used during tool labeling. It keeps behavior unchanged while reducing per-call overhead in
get_file_contentssecrecy checks and baseline scope inference.What does this improve?
Lower allocation pressure in frequently executed labeling paths.
Why is this valuable?
These paths run on nearly every
label_resource/label_responsedispatch, so small per-call savings compound quickly.Implementation approach:
check_file_secrecynow uses lazy path iteration (labels/tool_rules.rs)Vec<&str>collection frompath_lower.split('/').collect().path_lower.split('/').any(...).last()withpath_lower.rsplit('/').next().infer_scope_for_baselinenow returnsCow<'_, str>(lib.rs)StringtoCow<'_, str>.repo_id,scope_names::GITHUB, and empty scope).search_*query extraction returns owned data.Cow::Borrowed/Cow::Ownedbehavior ininfer_scope_for_baseline.